//写入收货地址信息
const setAddress = (state) => {
  const {
    AddressList
  } = state
  const AddressListString = JSON.stringify(AddressList)
  localStorage.AddressList = AddressListString
}

//获取收货地址
const getAddresstList = () => {

  try {
    if (localStorage.AddressList == undefined) {
      localStorage.AddressList = []
    }
    return JSON.parse(localStorage.AddressList) || []
  } catch (e) {
    return [];
  }

}

// 不要忘记把state, mutations等暴露出去。
export default {
  namespaced: true,
  state: {
    AddressList: getAddresstList(),
  },
  mutations: {
    //设置收货地址信息
    changeAddressInfo(state, payload) {

      if (payload.formData.id < 1) {
        //新增操作
        console.log(state.AddressList)
        var number = state.AddressList.length;

        if (number < 1) {
          payload.formData.id = 1;
        } else {

          state.AddressList.sort((item) => {

          })
          payload.formData.id = state.AddressList[number - 1].id + 1

        }
        state.AddressList.push(payload.formData)
      } else {
        //修改操作
        var index = state.AddressList.findIndex((item) => {
          return item.id = payload.formData.id;
        })
        if (index > -1) {
          state.AddressList[index] = payload.formData;
          setAddress(state)
        }

      }

      setAddress(state)
    },
    //删除收货地址信息
    DelteAddressInfo(state, payload) {
      var index = state.AddressList.findIndex((item) => {
        return item.id = payload.id;
      })
      if (index > -1) {
        state.AddressList.splice(index, 1);
        setAddress(state)
      }
    },

  },
  actions: {

  },
  getters: {

  }

}
